<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Models\Activity;
use App\Models\ActivityRecord;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Str;
use App\Admin\Actions\BlindBoxAction\RefreshBlindBoxRate;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Storage;
use Encore\Admin\Controllers\AdminController;

class ActivityRecordsController extends AdminController
{
    protected $title = '活动报名记录列表';
    

    protected function grid()
    {
        $grid = new Grid(new ActivityRecord());
	
	    $grid->disableExport();
	    
	     $grid->column('activity_id', '活动名称')->display(function () {
	    	return isset($this->activity) ? "{$this->activity->name}" : '';
	    });
	     $grid->column('user_id', '会员')->display(function () {
	    	return $this->user ? $this->user->nameText(true) : '';
	    });
	    $grid->column('money', '增加积分');
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	   	$this->gridActions($grid);
        return $grid;
    }
    

   	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableDelete();
			//$actions->add(new RefreshBlindBoxRate());
		});
	}

    protected function detail($id)
    {
        $show = new Show(ActivityRecord::findOrFail($id));
        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new ActivityRecord());

	  	$form->select('activity_id','活动')->options(Activity::all()->pluck('name', 'id'));
	  	$form->select('user_id','会员')->options(User::all()->pluck('name', 'id'));

	    $form->number('money', '积分')
		    ->min(1)->default(1)
		    ->help('NOTE: 参加活动奖励积分');
		$form->radio('is_qd', '签到')->default(false)->options(config('site.bool'));

		 $this->formSaving($form);
        return $form;
    }

      protected function formSaving(Form $form)
    {
        $form->saving(function (Form $form) {
          account_balances($form->model()->user)->plusSystemTransferCoins($form->money);
        });
    }

   
}
